package com.atguigu.serviceedu.service.impl;

import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.EduVideo;
import com.atguigu.serviceedu.entity.vo.ChapterVo;
import com.atguigu.serviceedu.entity.vo.VideoVo;
import com.atguigu.serviceedu.mapper.EduChapterMapper;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    //获取课程小接的mapper
    @Autowired
    private EduVideoService eduVideoService;

    //通过课程id进行查询所有章节信息
    @Override
    public List<ChapterVo> getAllChapter(String courseId) {

        //1、查询所有章节信息
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        wrapperChapter.orderByAsc("title");
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);

        //2、查询出所有的小节信息
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        wrapperVideo.orderByAsc("title");
        List<EduVideo> eduVideos = eduVideoService.list(wrapperVideo);

        //3、封装
        //把同一课程的小节封装
        List<ChapterVo> finallyList = new ArrayList<>();
        //遍历所有的章节，封装为chaptervo放入集合
        for (int i = 0; i < eduChapters.size(); i++) {
            EduChapter eduChapter = eduChapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            //把章节放入集合
            finallyList.add(chapterVo);
            List<VideoVo> videolist = new ArrayList<>();
            //获取该章节小节信息
            for (int j = 0; j < eduVideos.size(); j++) {
                EduVideo eduVideo = eduVideos.get(j);
                if (eduChapter.getId().equals(eduVideo.getChapterId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videolist.add(videoVo);
                }
            }
            chapterVo.setChildren(videolist);
        }
        return finallyList;
    }

    /**
     * 根据章节id查询是否有小节
     */
    @Override
    public boolean getChapterNumByChpterId(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = eduVideoService.count(wrapper);
        if (count > 0) {
            throw new GuliException(20001,"该章节还有小节,不能删除");
        } else {
            //删除章节
            int i = baseMapper.deleteById(chapterId);
            if(i>0)return true;
            else return false;
        }

    }

    //根据课程id删除章节
    @Override
    public void deleteChapterByCourseId(String courseId) {

        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();

        wrapper.eq("course_id",courseId);

        baseMapper.delete(wrapper);
    }
}
